#!/bin/bash 
HOSTNAME="192.168.1.216"
PORT="3306"
USERNAME="sms"
PASSWORD="sms123"
DBNAME="sms_sharding"
SRC_TABLENAME="t_short_url"
DEST_TABLENAME="t_short_url_his"
CONDITION=""

create_table_sql="create table IF NOT EXISTS ${DEST_TABLENAME} like ${SRC_TABLENAME}"
mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD}  -D ${DBNAME} -e "${create_db_sql}"

backsql="insert into ${DEST_TABLENAME} select * from ${SRC_TABLENAME} u LEFT JOIN t_short_info i on  u.surl = i.surl where i.date <= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)"
mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e  "${backsql}"

deletesql="DELETE u from ${SRC_TABLENAME} u LEFT JOIN t_short_info i on  u.surl = i.surl where i.date <= DATE_SUB(CURDATE(), INTERVAL 6 MONTH)"
mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e  "${deletesql}"